package com.iretailer.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iretailer.mapper.UserMapper;
import com.iretailer.model.User;
import com.iretailer.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author xjwtt
 * @date 2018/8/21
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> {

    @Autowired
    private UserMapper userMapper;

    public IPage<User> selectUserPage(Page<User> page, Wrapper wrapper) {
        if (page == null) {
            page = new Page<>(1, Long.MAX_VALUE);
        }
        return page.setRecords(userMapper.selectUserPage(page, wrapper));
    }

    /**
     * 用户注册 name userpwd usercode
     *
     * @param user
     * @return
     */
    public boolean register(User user) {
        if (user != null) {
            String pa = MD5Util.get32Md5(user.getUserpwd());
            user.setUserpwd(pa);
            userMapper.insert(user);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 用户登录
     *
     * @param user
     * @return
     */
    public User login(User user) {
        if (user != null) {
            user.setUserpwd(MD5Util.get32Md5(user.getUserpwd()));
            return userMapper.selectOne(new QueryWrapper<User>().lambda()
                    .eq(User::getUsercode, user.getUsercode())
                    .eq(User::getUserpwd, user.getUserpwd()));

        } else {
            return null;
        }
    }
}
